package MysqlDataSource.MYc3p0;


import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.util.ResourceBundle;

public class MyTestC3p0 {

    @Test
    //测试手动创建c3p0数据源（连接池）
    public void test01() throws Exception {
        //1.创建数据源的对象ComboPooledDataSource();
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //设置基本的连接参数

        //驱动
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        //地址
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        //拿到资源进行使用 获取资源
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();//关闭 归还到数据源当中

    }

    @Test
    //测试手动创建druid数据源（连接池）
    public void test02() throws Exception {
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

    @Test
    //测试手动创建c3p0数据源（加载properties配置文件）
    public void test03() throws Exception {
        //1.读取配置文件
        ResourceBundle rb = ResourceBundle.getBundle("jdbc");
        //2.赋值
        String driver=rb.getString("jdbc.driver");
        String url=rb.getString("jdbc.url");
        String username=rb.getString("jdbc.username");
        String password=rb.getString("jdbc.password");
        //3.创建数据源对象 设置连接参数
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        //4.获取资源
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        //5.关闭
        connection.close();

    }



}
